//
//  CameraSettingVM.swift
//  WatermarkCamera
//
//  Created by 赵新 on 8/7/2024.
//

import UIKit
import RxRelay
import AxcBedrock

extension CameraSettingVM {
    /// 相机设置
    enum CellType: Int, CaseIterable {
        /// 保存无水印
        case saveNoneWatermarkImage
        /// 辅助线样式
        case assistLineStyle
        /// 服务
        case serviceAgreement
        /// 隐私
        case privacyAgreement
    }

    /// 协议类
    struct Agreement {
        var urlStr: String
        var name: String

        var url: URL {
            return urlStr.axc.url
        }
    }

    /// 用户协议地址
    static var ServiceAgreement: Agreement {
        return .init(urlStr: "https://h5hybridprod.yupaowang.com/article-out?id=1811337856036339794",
                     name: "用户协议")
    }

    /// 隐私政策地址
    static var PrivacyAgreement: Agreement {
        return .init(urlStr: "https://h5hybridprod.yupaowang.com/article-out?id=1811337751782727750",
                     name: "隐私政策")
    }
}

// MARK: - [CameraSettingVM.CellModel]

extension CameraSettingVM {
    class CellModel {
        // Lifecycle

        init(title: String,
             style: Style,
             rowHeight: CGFloat = 57,
             cellType: CellType) {
            self.title = title
            self.style = style
            self.rowHeight = rowHeight
            self.cellType = cellType
        }

        // Internal

        enum Style {
            /// 箭头
            case arrow

            /// 纯文字
            case text(_ text: String,
                      color: UIColor = "8A8A99".axc.uiColor,
                      font: UIFont,
                      direction: AxcDirection)

            /// 纯开关
            case `switch`(_ isOn: Bool,
                          redPoints: Bool = false)

            /// 文字开关
            case textSwitch(_ text: String,
                            color: UIColor = "8A8A99".axc.uiColor,
                            font: UIFont,
                            direction: AxcDirection,
                            isOn: Bool,
                            redPoints: Bool = false)

            /// 标题后缀跟图片带文字
            case titleImageText(_ image: UIImage,
                                imageSize: CGSize,
                                text: String,
                                color: UIColor = "8A8A99".axc.uiColor,
                                font: UIFont,
                                direction: AxcDirection)
        }

        /// 标题
        var title: String
        /// 样式
        var style: Style
        /// 高度
        var rowHeight: CGFloat

        var cellType: CellType
    }
}

// MARK: - [CameraSettingVM]

class CameraSettingVM: NSObject {
    // MARK: 其他

    /// 当前版本是否为最新版
    static var currentIsNewVersion: Bool?

    // MARK: 输出

    /// 数据列表
    let dataListRelay: BehaviorRelay<[[CellModel]]> = .init(value: [])

    static func g0_defaultList() -> [CellType] {
        var group_0_defaultList: [CellType] = [
            .saveNoneWatermarkImage, // 保存无水印
            .assistLineStyle, // 辅助线设置
        ]
        return group_0_defaultList
    }

    static func g1_defaultList() -> [CellType] {
        var group_1_defaultList: [CellType] = []
        // 协议
        group_1_defaultList.append(contentsOf: [
            .serviceAgreement, // 用户协议
            .privacyAgreement, // 隐私政策
        ])
        return group_1_defaultList
    }

    /// 检查是否有某一种Cell，用于外部处理
    static func CheckUseCell(type: CellType) -> Bool {
        for cellType in g0_defaultList() {
            if cellType == type {
                return true
            }
        }
        for cellType in g1_defaultList() {
            if cellType == type {
                return true
            }
        }
        return false
    }

    /// 加载数据
    func loadList() {
        var list: [[CellModel]] = []
        // 第一组============
        let group_0 = toItemList(caseList: Self.g0_defaultList())
        list.append(group_0)

        // 第二组===============
        let group_1 = toItemList(caseList: Self.g1_defaultList())
        list.append(group_1)

        // 通知刷新
        dataListRelay.accept(list)
    }

    /// 转换成Model实体
    func toItemList(caseList: [CellType]) -> [CellModel] {
        let commonSettings = PreferenceSettingsManager.shared.cameraSetting
        var models: [CellModel] = []
        caseList.forEach { cas in
            switch cas {
            case .saveNoneWatermarkImage:
                let model = CellModel(title: "拍照时保存无水印原图",
                                      style: .textSwitch("保存两张图：1张无水印，1张有水印",
                                                         font: 14.axc.uiFont,
                                                         direction: .bottom,
                                                         isOn: commonSettings.saveNoneWatermarkImage),
                                      rowHeight: 79, cellType: .saveNoneWatermarkImage)
                models.append(model)

            case .assistLineStyle:
                let rawValue = commonSettings.assistLineStyle
                let cas = HomeControlAssistLineLayer.AssistLineStyle(rawValue: rawValue) ?? .none
                let model = CellModel(title: "拍摄辅助线",
                                      style: .text(cas.info.name,
                                                   font: 17.axc.uiFont,
                                                   direction: .right),
                                      cellType: .assistLineStyle)
                models.append(model)

            case .serviceAgreement:
                let model = CellModel(title: Self.ServiceAgreement.name,
                                      style: .arrow,
                                      cellType: .serviceAgreement)
                models.append(model)

            case .privacyAgreement:
                let model = CellModel(title: Self.PrivacyAgreement.name,
                                      style: .arrow,
                                      cellType: .privacyAgreement)
                models.append(model)
            }
        }
        return models
    }

    /// 更新开关类设置
    func updateSwitchSetting(cellType: CellType,
                             isOn: Bool) {
        let cameraSetting = PreferenceSettingsManager.shared.cameraSetting
        switch cellType {
        case .saveNoneWatermarkImage: cameraSetting.saveNoneWatermarkImage = isOn
        default: break
        }
        PreferenceSettingsManager.updateCameraSettings(model: cameraSetting)
    }

    /// 更新拍摄辅助线设置
    func updateAssistLineStyle(_ style: HomeControlAssistLineLayer.AssistLineStyle) {
        let cameraSetting = PreferenceSettingsManager.shared.cameraSetting
        cameraSetting.assistLineStyle = style.rawValue
        PreferenceSettingsManager.updateCameraSettings(model: cameraSetting)
        loadList()
    }
}
